﻿using App.Dal;
using App.Model;
using App.Untiys;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Win.Dal
{
    public class ProDepartServices
    {
        static SqlSugarScope dbHelper = SqliteSugarHelper.Db;

        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static ProDepart Info(ProDepart model)
        {
            return dbHelper.Queryable<ProDepart>()
                .WhereIF(model.Id > 0, m => m.Id == model.Id)
                .First();
        }

        /// <summary>
        /// 判断名称是否重复
        /// </summary>
        /// <param name="id"></param>
        /// <param name="DepartName"></param>
        /// <returns></returns>
        public static bool Isrepeat(int id, string DepartName = "")
        {
            if (id <= 0)
                return dbHelper.Queryable<ProDepart>().Where(m => m.DepartName == DepartName).Any();
            else
                return dbHelper.Queryable<ProDepart>().Where(m => m.DepartName == DepartName && m.Id != id).Any();
        }

        /// <summary>
        /// 获取列表  
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static List<ProDepart> GetList()
        {
            var sugarObj = dbHelper.Queryable<ProDepart>()
                .Where(m => m.IsDelete == 0);
            sugarObj = sugarObj.OrderBy(m => m.AddTime, OrderByType.Desc);
            return sugarObj.ToList();
        }

        /// <summary>
        /// 获取列表 分页
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static List<ProDepart> GetPageList(QueryPageModel model, ref int totalCount)
        {
            var sugarObj = dbHelper.Queryable<ProDepart>()
                .Where(m => m.IsDelete == 0)
                .WhereIF(!string.IsNullOrEmpty(model.KeyWord), od => od.DepartName.Contains(model.KeyWord));
            sugarObj = sugarObj.OrderBy(m => m.AddTime, OrderByType.Desc);
            return sugarObj.ToPageList(model.PageIndex, model.PageSize, ref totalCount);
        }

        /// <summary>
        /// 新增/编辑
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool Edit(ProDepart model)
        {
            model.AddTime = DateTime.Now;
            model.IsDelete = 0;
            model.Disable = 0;
            model.Remarks = "";
            if (model.Id <= 0)
                return dbHelper.Insertable<ProDepart>(model).ExecuteCommand() > 0;
            return dbHelper.Updateable<ProDepart>(model).IgnoreColumns(m => new
            {
                m.AddTime,
                m.IsDelete,
                m.Id,
                m.AddUser
            }).Where(m => m.Id == model.Id).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool Delete(int id)
        {
            return dbHelper.Updateable<ProDepart>()
                        .SetColumns(it => new ProDepart() { IsDelete = 1 })
                        .Where(it => it.Id == id)
                        .ExecuteCommand() > 0;
        }
    }
}
